<template>
  <div class="zw">
    <div class="body">
      <div class="title">真唯科技售后设备返修查询</div>
      <div class="tool">
        <el-input @keydown.enter="onQuery" v-model="searchKey" placeholder="输入产品SN或快递单号查询,用分号分隔可同时查询多条！"></el-input>
        <el-button style="margin-left: 12px;" type="primary" @click="onQuery">查询</el-button>
      </div>
      <div class="data">
        <el-scrollbar class="scroll">
          <el-collapse accordion expand-icon-position="left">
            <el-collapse-item v-for="item in dataList" :name="item.receiveNumber" :key="item.receiveNumber">
              <template #title>
                <div class="data-title">
                  <span class="data-title-item">快递单号：{{ item.receiveNumber }}</span>
                  <span class="data-title-item">快递接收日期：{{ item.receiveDate.replace("00:00:00", "").replace("T",
                    "").replace("1900-01-01", "") }}</span>
                  <span
                    :class="['data-title-item', { 'notfinish': item.repairState !== '已完成' }, { 'finished': item.repairState === '已完成' }]">维修状态:
                    {{
                      item.repairState }}</span>
                </div>
              </template>
              <el-table :data="item.children" stripe style="width: 100%" border max-height="320" size="small">
                <el-table-column prop="productName" label="产品型号" width="180" />
                <el-table-column prop="productSn" label="产品SN" width="180" />
                <el-table-column prop="repairState" label="维修状态" />
                <el-table-column prop="repairSite" label="维修站点" />
                <el-table-column prop="returnNumber" label="返回快递单号" />
                <el-table-column prop="returnDate" label="返回快递日期" />
              </el-table>
            </el-collapse-item>
          </el-collapse>
        </el-scrollbar>
      </div>
    </div>
    <el-dialog :model-value="visible" title="请输入验证码" width="500" :close-on-press-escape="false"
      :close-on-click-modal="false" :show-close="false">
      <div style="display: flex; align-items: center; justify-content: center;">
        <el-input ref="captchaRef" @keydown.enter="onSubmit" v-model="captcha" placeholder="验证码"></el-input>
        <img style="width: 120px;margin-left: 12px;" :src="captchaUrl + '?t=' + timestamp" alt="验证码"
          @click="refreshCaptcha" />
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="visible = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import type { CustomerSearchDTO } from '@/server/afterSale/types';
import ApiTree from '@/server/api-tree';
import { ElMessage, type InputInstance } from 'element-plus';
import { ref, useTemplateRef } from 'vue';

const dataList = ref<{ receiveNumber: string, receiveDate: string, repairState: string, children: CustomerSearchDTO[] }[]>([])
const visible = ref(false)
const captchaUrl = ref(`${import.meta.env.VITE_APP_API_URL}/captcha/generate`)
const timestamp = ref(new Date().getTime()) // 初始时间戳
const refreshCaptcha = () => {
  timestamp.value = new Date().getTime(); // 更新时间戳，避免缓存
}
const captcha = ref("")
const searchKey = ref("")
const captchaRef = useTemplateRef<InputInstance>("captchaRef");
const onQuery = () => {
  captcha.value = "";
  refreshCaptcha();
  captchaRef.value?.focus();
  visible.value = true;
}
const onSubmit = async () => {
  dataList.value = [];
  const key = searchKey.value.trim();
  const c = captcha.value.trim();
  const { success, data } = await ApiTree.afterSaleOrder.customerSearch(key, c);
  if (success) {
    if (data.length === 0) {
      ElMessage.error("未查询到数据！");
      return;
    }
    const receiveNumberList = [...new Set(data.map(x => x.receiveNumber))];
    receiveNumberList.forEach(receiveNumber => {
      const list = data.filter(x => x.receiveNumber === receiveNumber);
      dataList.value.push({
        receiveNumber: receiveNumber,
        receiveDate: list.length > 0 ? list[0].receiveDate : '',
        repairState: list.every(x => x.repairState === "维修完成") ? "已完成" : "维修中",
        children: list.map(x => {
          return {
            ...x,
            returnDate: x.returnDate.replace("00:00:00", "").replace("T", "").replace("1900-01-01", "")
          }
        })
      })
    })
    visible.value = false;
  }
}
</script>
<style lang="scss" scoped>
.zw {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f1f1f1;

  .body {
    width: 60%;
    height: 80%;
    display: flex;
    overflow: hidden;
    flex-direction: column;
    align-items: center;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 5px;

    .title {
      height: 48px;
      width: 100%;
      line-height: 48px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
    }

    .tool {
      width: 100%;
      height: 48px;
      display: flex;
      align-items: center;
      padding: 0 20px;

    }

    .data,
    .scroll {
      width: 100%;
      height: calc(100vh * 0.8 - 96px - 40px);
    }

    .data {
      padding: 20px;

      .finished {
        color: #05d447;
      }

      .notfinish {
        color: #078fce;
      }
    }

    .sub-data {
      padding: 0 40px;
    }

    .data-title {
      display: flex;
      flex-direction: row;

      :not(:first-child) {
        margin-left: 20px;
      }

      &-item {
        font-size: 14px;
        font-weight: 600;


      }

    }
  }
}
</style>
